package edu.dongthang.screens;

import java.util.Locale;

import edu.dongthang.screens.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SettingScreen extends Activity {
	Locale locale;
	final static Locale en_locale = Locale.ENGLISH;
	final static Locale vn_locale = new Locale("vn");
	Spinner spinner;
	String selected =  null;
	ArrayAdapter<CharSequence> adapter;
	SharedPreferences pref;
	Editor editor;
	String lang = "en_US";
	int key_lang;
	public static final String PREFS_NAME = "MyPrefsFile";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.actitivy_setting);
		spinner = (Spinner) findViewById(R.id.spinner_language);
		TextView tv = (TextView)findViewById(R.id.lang);
		tv.setTextColor(Color.GRAY);
		//Typeface font1 = Typeface.createFromAsset(getAssets(),"fonts/HLT bauserif.ttf");
		
		pref = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
		editor = pref.edit();
		adapter = ArrayAdapter.createFromResource(this, R.array.language,
				android.R.layout.simple_spinner_item);
		//simple_spinner_dropdown_item
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		
		key_lang = pref.getInt("key_lang", 0);
		
		spinner.setSelection(key_lang);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				switch (arg2) {
				case 0:
					setLocate(en_locale);
					editor.clear();
				    editor.putString("language", Locale.getDefault().toString());
				    editor.putInt("key_lang", arg2);
				    editor.commit();
					break;
				case 1:					
					setLocate(vn_locale);
					editor.clear();
				    editor.putString("language", Locale.getDefault().toString());
				    editor.putInt("key_lang", arg2);
				    editor.commit();
					break;
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}

		});

	}

	public void setLocate(Locale locale) {
		Locale.setDefault(locale);
		Configuration config = new Configuration();
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config,
				getBaseContext().getResources().getDisplayMetrics());
	}
	
	
}
